package com.drinks.yiniang.ui.fragment.community;


import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.viewpager.TabFragmentAdapter;
import com.drinks.yiniang.common.listener.OnHomeNavigationBarClickListener;
import com.drinks.yiniang.ui.widget.nav.NavigationHomeBarLayout;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 社区页面
 * @author han
 * A simple {@link Fragment} subclass.
 */
public class CommunityPageFragment extends BaseFragment implements OnHomeNavigationBarClickListener, TabLayout.OnTabSelectedListener {

    private NavigationHomeBarLayout nav_bar_community_page;
    private TabLayout tab_community_page;
    private ViewPager vp_community_page;

    private CommunityFollowFragment mFollowFragment;
    private CommunityHotFragment mHotFragment;
    private CommunityActFragment mActivityFragment;

    private OnHomeNavigationBarClickListener onHomeNavgationBarClickListener;

    private TabFragmentAdapter mTabFragmentAdapter;

    private List<BaseLazyFragment> mFragments;
    private String[] mTabs;

    public CommunityPageFragment() {
        // Required empty public constructor
    }

    public static CommunityPageFragment newInstance() {
        return new CommunityPageFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            onHomeNavgationBarClickListener = (OnHomeNavigationBarClickListener) context;
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + " must implement OnHomeNavgationBarClickListener");
        }
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_community_page;
    }

    @Override
    protected void initView(View mView) {
        nav_bar_community_page = mView.findViewById(R.id.nav_bar_community_page);

        tab_community_page = mView.findViewById(R.id.tab_community_page);

        vp_community_page = mView.findViewById(R.id.vp_community_page);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        initTabAndViewPager();
    }

    //初始化viewpager数据
    private void initTabAndViewPager() {
        mTabs = new String[]{getString(R.string.text_follow), getString(R.string.text_hot), getString(R.string.text_activity)};

        for(int i = 0; i < mTabs.length; i++) {
            tab_community_page.addTab(tab_community_page.newTab().setText(mTabs[i]), i == 1);
        }

        mFragments = new ArrayList<>(mTabs.length);

        mFollowFragment = CommunityFollowFragment.newInstance();
        mHotFragment = CommunityHotFragment.newInstance();
        mActivityFragment = CommunityActFragment.newInstance();

        mFragments.add(mFollowFragment);
        mFragments.add(mHotFragment);
        mFragments.add(mActivityFragment);

        mTabFragmentAdapter = new TabFragmentAdapter(getChildFragmentManager(), mFragments, mTabs);

        vp_community_page.setOffscreenPageLimit(2);
        vp_community_page.setAdapter(mTabFragmentAdapter);
        vp_community_page.setCurrentItem(1);
        tab_community_page.setupWithViewPager(vp_community_page);
    }

    @Override
    protected void listener() {
        nav_bar_community_page.setOnNavgationBarClickListener(this);
        tab_community_page.addOnTabSelectedListener(this);
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    public void onAddress(View v) {
        if(onHomeNavgationBarClickListener != null) {
            onHomeNavgationBarClickListener.onAddress(v);
        }
    }

    @Override
    public void onSearchClick(View v) {
        if(onHomeNavgationBarClickListener != null) {
            onHomeNavgationBarClickListener.onSearchClick(v);
        }
    }

    @Override
    public void onMessage(View v) {
        if(onHomeNavgationBarClickListener != null) {
            onHomeNavgationBarClickListener.onMessage(v);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
